package com.neuedu.vuejsserver.service;

import com.neuedu.dao.DeptDao;
import com.neuedu.entity.Dept;
import com.neuedu.vuejsserver.domain.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 项目：      jweb
 * 类名:       DeptService
 * 创建时间：  2023/11/24 13：56
 * 描述 :     使用axios
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
public class DeptService extends HttpServlet {

    private DeptDao dao = new DeptDao();


    public Page queryList(HttpServletRequest request){
        int pageNo = 1;
        int size = 10;
        String paramPageNo = request.getParameter("pageNo");
        String paraSize = request.getParameter("size");
        if(!Objects.isNull(paramPageNo)){
            pageNo = Integer.parseInt(paramPageNo);
        }
        if(!Objects.isNull(paraSize)){
            size = Integer.parseInt(paraSize);
        }



        List paraList = new ArrayList();
        String where  = "where 1=1   ";
        String dname = request.getParameter("dname");
        if(!Objects.isNull(dname)){
            where += " and dname like concat('%',?,'%')";
            paraList.add(dname);
        }
        Object[] params = paraList.toArray();
        long total  = dao.selectCount(where,params);
        List<Dept> data = dao.selectPage(where,pageNo,size,params);

        return  Page.getInstatnce(pageNo ,size , data , total);
    }


    public boolean save(HttpServletRequest req) {


         Dept dept = new Dept(
                 Integer.parseInt(req.getParameter("deptno")),
                 req.getParameter("dname"),
                 req.getParameter("loc")
                 );

        return  dao.insert(dept) >0;
    }

    public boolean update(HttpServletRequest req) {
        Dept dept = new Dept(
                Integer.parseInt(req.getParameter("deptno")),
                req.getParameter("dname"),
                req.getParameter("loc")
        );

        return  dao.updateById(dept) >0;
    }

    public Dept queryById(HttpServletRequest req) {
        String deptno = req.getParameter("deptno");
        String where = " where deptno = ?";
        Object params[] = {deptno};
        return dao.selectOne(where,params);
    }


    /**
     * 根据主键删除
     * @param req
     * @return
     */
    public boolean deleteById(HttpServletRequest req) {
        String deptno = req.getParameter("deptno");

        return dao.deleteById(Integer.parseInt(deptno))>0;
    }
}
